﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using Aspose.Cells;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;



public partial class Report_UserControl_uc_MatHang_TonKho : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private DataTable ConvertToDataTable<ZD_PRC_RPT_MATHANG_TONKHO_THEOKHOResult>(List<ZD_PRC_RPT_MATHANG_TONKHO_THEOKHOResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_RPT_MATHANG_TONKHO_THEOKHOResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_RPT_MATHANG_TONKHO_THEOKHOResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    protected void btnXuat_Click(object sender, EventArgs e)
    {
        OutReport outRPT = new OutReport();
        //lay database
        ReportBO rpt = new ReportBO();
        List<ZD_PRC_RPT_MATHANG_TONKHO_THEOKHOResult> result = new List<ZD_PRC_RPT_MATHANG_TONKHO_THEOKHOResult>();
        result = rpt.Report_MatHangTonKho();
        DataTable dt1 = ConvertToDataTable(result);
        DataTable dt2 = new DataTable();
        DataColumn column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "DateReport";
        dt2.Columns.Add(column);

        DataRow row = dt2.NewRow();
        row["DateReport"] = DateTime.Now.ToString("dd/MM/yyyy");
        dt2.Rows.Add(row);
        // datatable 1
        DataTable Report1 = new DataTable();
        // datatable 2
        DataTable Report2 = new DataTable();
        Report1 = dt1;
        Report2 = dt2;
        Report1.TableName = "Report";
        Report2.TableName = "Date";
        DataTable[] arrTable = { Report1, Report2 };
        outRPT.OutReportToScreen(MapPath("~/RPTTemplate/RPT_MATHANG_TONKHO_THEOKHO_TEMPLATE.xls"), arrTable, "_MatHang_TonKho.xls");
    }   
}
